package com.man.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;



import javax.servlet.ServletException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.man.daoService.DaoService;
import com.man.entity.User;
import com.opensymphony.xwork2.ActionSupport;


public class FileAction extends ActionSupport {
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DaoService daoService=new DaoService();
	 public String delete()
	 {
		 User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		 com.man.entity.File file= new com.man.entity.File();   
		 file.setUserName(user.getUserName());
    	 file.setTitle(getTitle());
    	 com.man.entity.File fileE= daoService.fileOneSelect(file);
    	 File file1=new File(fileE.getFilepath());
    	 file1.delete();
    	 Boolean b= daoService.filedelete(file);
    	 if(b)
    	 return "success";
    	 else
    	 { ServletActionContext.getRequest().getSession().setAttribute("msg", "系统故障,文件删除失败");
    		 return "msg";
    	 }
	 }
     public String	insert() 
     {	 
//    	 System.out.println(getTitle());
	  	 String size=null,filepath=null,path=ServletActionContext.getServletContext().getRealPath("/file/");
	  	 int s=(int) getFile().length();
	  	 size=s+"B";
	  	 User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
    	 com.man.entity.File file= new com.man.entity.File();   
    	 file.setUserName(user.getUserName());
    	 file.setTitle(getTitle());
    	 file.setName(getFileFileName());
    	 file.setContentType(getFileContentType());
    	 file.setFilepath(path+"\\"+getFileFileName());
    	 file.setSize(size);
    	
    	 if(daoService.fileOneSelect(file)!=null){
    		 ServletActionContext.getRequest().getSession().setAttribute("msg", "该文件标题已存在，请更换标题");    		 
    		 return "msg";
    	 }
    	 else{
    		 try {
        		 File file2=new File(path);
        		 if(!file2.exists())
        			 file2.mkdirs();
        		 FileOutputStream outputStream=new FileOutputStream(file.getFilepath());
    			InputStream inputStream=new FileInputStream(getFile());
    			byte[] b=new byte[1024];
    			while(inputStream.read(b)>0)
    			{
    				outputStream.write(b);
    			}
    			inputStream.close();
    			outputStream.close();
    			getFile().delete();
    			Boolean x=daoService.fileInsert(file);
    			if(x)
    			return "success";
    			else
    				return "error";
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();return "error";
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();return "error";
    		}
    	 	 
    	 }
    	 
     }
     public void select()
 	{User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
 	 com.man.entity.File file=new com.man.entity.File();
 	 file.setUserName(user.getUserName());
 	 List<com.man.entity.File> list= daoService.fileSelect(file);
 	 try {
 		String s= JSONUtil.serialize(list);
 		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
// 		System.out.println(s);
 		ServletActionContext.getResponse().getWriter().print(s);
 	} catch (JSONException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	} catch (IOException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	}
 	
 	}
     public void oneSelect() throws IOException, ServletException
 	{	
 	 User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
 	 com.man.entity.File file=new com.man.entity.File();
 	 file.setUserName(user.getUserName());
 	 file.setTitle(getTitle());
 	 
 	 com.man.entity.File file2= daoService.fileOneSelect(file);
 	 if(file2.getFilepath()!=null)
 	 {
 		File file3=new File(file2.getFilepath());
 		if(!file3.exists())
 		{
 			ServletActionContext.getRequest().getRequestDispatcher("section/fileManage.jsp").forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
// 			return "no";
 		}
 		else{
 				
// 		ServletActionContext.getResponse().setContentType(file2.getContentType()+";charset=utf-8");
 		ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+new String(file2.getName().getBytes("utf-8"),"ISO8859-1"));
// 		//读取要下载的文件，保存到文件输入流
 		FileInputStream in = new FileInputStream(file2.getFilepath());
// 		//创建输出流
 		OutputStream out = ServletActionContext.getResponse().getOutputStream();
// 		//创建缓冲区
 		byte buffer[] = new byte[1024];
 		int len = 0;
 		//循环将输入流中的内容读取到缓冲区当中
 		while((len=in.read(buffer))>0){
 		//输出缓冲区的内容到浏览器，实现文件下载
 		out.write(buffer, 0, len);
 		}
 		//关闭文件输入流
 		in.close();
 		//关闭输出流
 		out.close();
 		//return "success";
 	 }
 	 }
// 	 else
// 		return "no";
 
 	 
 	}
     private String title;
     public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	private File file;  
 	// myFileContentType属性用来封装上传文件的类型  
     private String fileContentType;  
     // myFileFileName属性用来封装上传文件的文件名  
     private String fileFileName;  
 	public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}

}
